//
//  AuthenService.m
//  HxgProject
//
//  Created by hxg on 2018/3/27.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "AuthenService.h"
#import "NetworkManager.h"

#import "ContactInfo.h"
#import "ReputationDetail.h"
#import "ResObject.h"
#import "PersonInfo.h"
#import "HxgInterfaceDefine.h"

#import "BankInfo.h"
#import "CompanyInfo.h"

@implementation AuthenService

- (void)getUserInfoWithCallBack:(void(^)(PersonInfo *person)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kGetAppUserInfo];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    [man getURL:urlInfo parameters:params success:^(id responseObject) {
        
        NSLog(@"userInfo ===>%@",responseObject);
        NSDictionary *dic = [responseObject objectForKey:@"userBaseInfo"];
        
        if ([responseObject objectForKey:@"userInfo"] != [NSNull null]
            && nil != dic)
        {
            PersonInfo *personInf = [[PersonInfo alloc] initWithDic:dic];
            callBack(personInf);
        } else {
            callBack(nil);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        callBack(nil);
    }];
}


- (void)setUserInfoWithPerson:(PersonInfo *)person
                withCallBack:(void(^)(BOOL isSuccess))callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kSaveOrUpdateUserInfo];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    params[@"name"] = person.name;
    params[@"idCard"] = person.idCard;
    params[@"qq"] = person.qq;
    params[@"webChat"] = person.webChat;
    params[@"chsiAccount"] = person.chsiAccount;
    params[@"chsiPwd"] = person.chsiPwd;
    params[@"operatorPwd"] = person.operatorPwd;
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        if ([[responseObject objectForKey:@"Success"]  isEqual: kRequestSuccess])
        {
            callBack(YES);
        } else  {
            callBack(NO);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        callBack(NO);
    }];
}

- (void)saveOrUpdateBankInfo:(BankInfo *)bankInfo
                withCallBack:(void(^)(BOOL isSuccess))callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kSaveOrUpdateBankInfo];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    params[@"bankAccount"] = bankInfo.bankAccount;
    params[@"bank"] = bankInfo.bank;
    params[@"subBank"] = bankInfo.subBank;
    
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        if ([[responseObject objectForKey:@"Success"]  isEqualToString:kRequestSuccess])
        {
            callBack(YES);
        }
        else
        {
            callBack(NO);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        callBack(NO);
    }];
}

- (void)getBankInfoWithCallBack:(void(^)(BankInfo *bankInfo)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kGetBankInfo];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    
    [man getURL:urlInfo
     parameters:params
        success:^(id responseObject) {
            NSLog(@"responseObject===/////=:%@",responseObject);
            NSDictionary *dic = [responseObject objectForKey:@"bankInfo"];
            
            if ([responseObject objectForKey:@"bankInfo"] != [NSNull null]
                && nil != dic)
            {
                BankInfo *bankInfo = [[BankInfo alloc] initWithDic:dic];
                callBack(bankInfo);
            } else {
                callBack(nil);
            }
        } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
            NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
            callBack(nil);
        }];
}


- (void)saveOrUpdateCompanyInfo:(CompanyInfo *)company
                   withCallBack:(void(^)(BOOL isSuccess))callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kSaveOrUpdateCompanyInfo];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    params[@"companyName"] = company.companyName;
    params[@"companyAddress"] = company.companyAddress;
    params[@"companyLeaderPhone"] = company.companyLeaderPhone;
    
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        if ([[responseObject objectForKey:@"Success"]  isEqual: kRequestSuccess])
        {
            callBack(YES);
        }
        else
        {
            callBack(NO);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        callBack(NO);
    }];
}

- (void)getCompanyInfoWithCallBack:(void(^)(CompanyInfo *info)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kGetUserCompanyInfo];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    
    [man getURL:urlInfo
     parameters:params
        success:^(id responseObject) {            
            NSDictionary *dic = [responseObject objectForKey:@"CompanyInfo"];
            if ([responseObject objectForKey:@"CompanyInfo"] != [NSNull null]
                && nil != dic)
            {
                CompanyInfo *companyInfo = [[CompanyInfo alloc] initWithDic:dic];
                callBack(companyInfo);
            } else {
                callBack(nil);
            }
        } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
            NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
            callBack(nil);
        }];
}

- (void)saveContacts:(ContactInfo *)contactInfo
        withCallBack:(void(^)(ResObject *res)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kSaveUserContacts];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    params[@"contactName1"] = contactInfo.contactName1;
    params[@"contactPhone1"] = contactInfo.contactPhone1;
    params[@"contactName2"] = contactInfo.contactName2;
    params[@"contactPhone2"] = contactInfo.contactPhone2;

    ResObject *myRes = [[ResObject alloc] init];
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        if ([[responseObject objectForKey:kSuccess]  isEqual: kRequestSuccess]) {
            myRes.isSuccess = YES;
            callBack(myRes);
        } else {
            myRes.isSuccess = NO;
            myRes.errMsg = [responseObject objectForKey:@"errMsg"];
            callBack(myRes);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        myRes.isSuccess = NO;
        myRes.errMsg = @"操作失败!";
        callBack(myRes);
    }];
}

- (void)getContactsWithCallBack:(void(^)(ContactInfo *info)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kGetUserContacts];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    
    [man getURL:urlInfo
     parameters:params
        success:^(id responseObject) {
            NSLog(@"responseObject===/////=:%@",responseObject);
            
            NSDictionary *dic = [responseObject objectForKey:@"UserContactInfo"];
            if ([responseObject objectForKey:@"UserContactInfo"] != [NSNull null]
                && nil != dic)
            {
                ContactInfo *contactInfo = [[ContactInfo alloc] initWithDic:dic];
                callBack(contactInfo);
            } else {
                callBack(nil);
            }
        } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
            NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
            callBack(nil);
        }];
}

- (void)saveReputationDetail:(ReputationDetail *)reputationDetail
                withCallBack:(void(^)(BOOL isSuccess)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kSaveReputationDetail];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
  
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    params[@"zfbAccount"] = reputationDetail.zfbAccount;
    params[@"zfbPwd"] = reputationDetail.zfbPwd;
    params[@"zfbFlag"] = reputationDetail.zfbFlag;
    params[@"tbAccount"] = reputationDetail.tbAccount;
    params[@"tbPwd"] = reputationDetail.tbPwd;
    params[@"tbFlag"] = reputationDetail.tbFlag;

    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        NSLog(@"xin yu ==>responseObject:%@",responseObject);
        if ([[responseObject objectForKey:@"Success"]  isEqual: kRequestSuccess]) {
            callBack(YES);
        } else {
            callBack(NO);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        callBack(NO);
    }];
}

- (void)getReputationDetailWithCallBack:(void(^)(ReputationDetail *rep)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Auther_kFindReputationDetail];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    
    [man getURL:urlInfo
     parameters:params
        success:^(id responseObject) {
            NSLog(@"ReputationDetail<<==>:%@",responseObject);
            
            NSDictionary *dic = (NSDictionary *)responseObject;
            if ([[dic objectForKey:@"Success"] isEqualToString:kRequestSuccess]) {
                
                ReputationDetail *reputation = [[ReputationDetail alloc] initWithDic:[dic objectForKey:@"message"]];
                callBack(reputation);
            } else {
                callBack(nil);
            }
            
        } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
            NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
            callBack(nil);
        }];
}



@end
